// /src/utils/logsUtil.js
const fs = require('fs');
const path = require('path');
const Logger = require('./logger/logger');
const { getRootPath } = require('./utils/path');
const { parseRetentionTime } = require('./utils/retention');
const { cleanLogsByRetention } = require('./utils/compress');
const { runInitialTasks } = require('./tasks/scheduler');
const { levelPriority } = require('./utils/levels');

// 获取日志目录路径
const projectRoot = getRootPath();
const logDirectory1 = path.join(projectRoot, 'logs'); // logs/年/月/日/level/年-月-日_level.log
const logDirectory2 = path.join(projectRoot, 'logs2');

// 获取日志保留时间配置
const logRetention = parseRetentionTime(process.env.LOG_RETENTION);

// 创建两个日志实例
const log = new Logger({ logDir: logDirectory1 });
const log2 = new Logger({ logDir: logDirectory2 });

// 定义任务函数
const compressTask = async () => {
  // 这里需要引入压缩函数
  const { compressYesterdayLogs } = require('./utils/compress');
  await compressYesterdayLogs(logDirectory1, levelPriority);
  await compressYesterdayLogs(logDirectory2, levelPriority);
};

const cleanTask = () => {
  cleanLogsByRetention(logDirectory1, logRetention);
  cleanLogsByRetention(logDirectory2, logRetention);
};

// 只设置定时任务，不在初始化时执行压缩和清理操作
const { scheduleTask } = require('./tasks/scheduler');
scheduleTask(compressTask, parseInt(process.env.COMPRESS_HOUR) || 2, parseInt(process.env.COMPRESS_MINUTE) || 0);
scheduleTask(cleanTask, parseInt(process.env.CLEAN_HOUR) || 3, parseInt(process.env.CLEAN_MINUTE) || 0);

module.exports = {
  Logger,
  log,
  log2
};